package middleware

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/sirupsen/logrus"
	//"github.com/lestrrat-go/file-rotatelogs"
	//"github.com/rifflock/lfshook"
	"math"
	"os"
	"time"
)

func Logger() gin.HandlerFunc{
	filePath := "log/ginlog.log"

	src, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE, 0755)
	if err != nil {
		fmt.Println("err:", err)
	}
	logger := logrus.New()

	logger.Out = src

	logger.SetLevel(logrus.DebugLevel)

	return func(c *gin.Context) {
		startTime := time.Now()
		c.Next()
		stopTime := time.Since(startTime)
		spendTime := fmt.Sprintf("%d ms",int(math.Ceil(float64(stopTime.Nanoseconds()/1000000.0))))
		hostName, err := os.Hostname()
		if err != nil {
			hostName = "unknown"
		}
		statusCode := c.Writer.Status()
		clientIp := c.ClientIP()
		userAgent := c.Request.UserAgent()
		dataSize := c.Writer.Size()
		if dataSize < 0 {
			dataSize = 0
		}
		method := c.Request.Method
		path := c.Request.RequestURI

		entry := logger.WithFields(logrus.Fields{
			"HostName":hostName,
			"Status": statusCode,
			"SpendTime":spendTime,
			"Ip":clientIp,
			"Method":method,
			"Path":path,
			"DataSize":dataSize,
			"Agent":userAgent,
		})

		if len(c.Errors) >0 {
			entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String())
		}

		if statusCode >= 500 {
			entry.Error()
		}	else if statusCode >= 400 {
			entry.Warn()
		}	else {
			entry.Info()
		}
	}
}
